///***********************************************************************
//	filename: 	4YsXMLParserEvent.cpp
//	created:	18/4/2012
//	author:		4Ys
//	
//	purpose:	Implements classes of XMLParserEvent with TinyXML, 
//				especially for Event.	            
//*************************************************************************/
//
//#include "4YsXMLParserEvent.h"
//
//#include <strstream>
//#include <time.h>
//
//namespace Dater
//{
//	//----------------------------------------------------------------------------//
//	XMLParserEvent::XMLParserEvent(const std::string& fileName):
//	XMLParser()
//	{
//		_className = "Event";
//		_xmlName = fileName;
//	}
//
//	//----------------------------------------------------------------------------//
//	void XMLParserEvent::insertEventNode(Event* anevent, int ID)
//	{
//		std::string sID = intToString(ID);
//		TiXmlElement* eventNode = insertNode(sID);
//		completeElement(eventNode, anevent);
//		return;
//	}
//
//	//----------------------------------------------------------------------------//
//	std::vector<Event*> XMLParserEvent::exportToEvent()
//	{
//		std::vector<Event*> result;
//		TiXmlElement* rootElement = _myXml->RootElement();
//		if(rootElement != NULL)//check the xml file isn't null.
//		{
//			std::cout << rootElement->Value() << std::endl;
//			TiXmlElement* startNode = rootElement->FirstChildElement();//startNode is the root of every Event data.	
//			while(startNode != NULL)
//			{
//				//initialize local variables for a certain Event.
//				std::string type, name, description;
//				unsigned int userID, eventID;
//				Time createTime, startTime, finishTime;
//				Reminding reminding;
//				TiXmlAttribute *IDAttribute = startNode->FirstAttribute();
//				eventID = std::atol(IDAttribute->Value());//assign eventID.
//				TiXmlElement* startElement = startNode->FirstChildElement();//startElement is the first element of the startNode.
//				if(startElement != startNode->LastChild() && startElement != NULL)//not null and it isn't reminding.
//				{	
//					type = startElement->FirstChild()->Value();//assign type.
//					startElement = startElement->NextSiblingElement();//point to next value.
//					userID = std::atol(startElement->FirstChild()->Value());//assign userID.
//					startElement = startElement->NextSiblingElement();//point to next value.
//					name = startElement->FirstChild()->Value();//assign name.
//					startElement = startElement->NextSiblingElement();//point to next value.
//					createTime = stringToTime(startElement->FirstChild()->Value());//assign createTime.
//					startElement = startElement->NextSiblingElement();//point to next value.
//					startTime = stringToTime(startElement->FirstChild()->Value());//assign startTime.
//					startElement = startElement->NextSiblingElement();//point to next value.
//					finishTime = stringToTime(startElement->FirstChild()->Value());//assign finishTime.
//					startElement = startElement->NextSiblingElement();//point to next value.
//					description = startElement->FirstChild()->Value();//assign description.
//					startElement = startElement->NextSiblingElement();//point to next value.
//				}
//				else if(startElement != NULL)
//				{
//					TiXmlElement* remindingElement = startElement->FirstChildElement();//remindingElement is is the first element of reminding.
//					reminding.setStartTime(stringToTime(remindingElement->FirstChild()->Value()));//assign startTime.
//					remindingElement = remindingElement->NextSiblingElement();//point to next value.
//					reminding.setRemindingInterval(atol(remindingElement->FirstChild()->Value()));//assign remindInterval.
//					remindingElement = remindingElement->NextSiblingElement();//point to next value.
//					reminding.setRemindTimes(atol(remindingElement->FirstChild()->Value()));//assign remindInterval.
//				}
//				startNode = startNode->NextSiblingElement();//point to next Event data in xml file.
//				Event* tempEvent = EventManager::getSingleton().addEvent(type, name, startTime, finishTime, description, userID, eventID);
//				result.push_back(tempEvent);
//			}			
//		}
//		return result;
//	}
//
//	//----------------------------------------------------------------------------//
//	void XMLParserEvent::setID(TiXmlElement* anelement, std::string pkeyname, Event* anevent)
//	{
//		anelement->SetAttribute(pkeyname, anevent->getEventID());
//		return;
//	}
//
//	//----------------------------------------------------------------------------//
//	void XMLParserEvent::createReminding(TiXmlElement* element, Event* anevent)
//	{
//		TiXmlElement *remindingElement = new TiXmlElement("Reminding");
//		element->LinkEndChild(remindingElement);
//		createElement(remindingElement, "startTime", timeToString(anevent->getReminding()->getStartTime()));
//		createElement(remindingElement, "remindInterval", intToString(anevent->getReminding()->getRemindInterval()));
//		createElement(remindingElement, "remindTimes", intToString(anevent->getReminding()->getRemindTimes()));
//		return;
//	}
//
//	//----------------------------------------------------------------------------//
//	void XMLParserEvent::completeElement(TiXmlElement* eventelement, Event* anevent)
//	{
//		createElement(eventelement, "type", anevent->getType());
//		createElement(eventelement, "userID", intToString(anevent->getUserID()));
//		createElement(eventelement, "name", anevent->getName());
//		createElement(eventelement, "createTime", timeToString(anevent->getCreateTime()));
//		createElement(eventelement, "startTime", timeToString(anevent->getStartTime()));
//		createElement(eventelement, "finishTime", timeToString(anevent->getFinishTime()));
//		createElement(eventelement, "description", anevent->getDescription());
//		createReminding(eventelement, anevent);
//		return;
//	}
//
//		//----------------------------------------------------------------------------//
//	std::string XMLParserEvent::intToString(int ID)
//	{
//		int iID = ID;
//		std::string sID;
//		std::strstream strStream;
//		strStream << iID;
//		strStream >> sID;
//		return sID;
//	}
//	
//	//----------------------------------------------------------------------------//
//	bool XMLParserEvent::readXmlFile()
//	{
//		/*try
//		{
//			TiXmlElement *rootElement = _myXml->RootElement();
//			if(rootElement != NULL)
//			{
//				std::cout << rootElement->Value() << std::endl;
//				TiXmlElement *startNode = rootElement->FirstChildElement();		
//				while(startNode != NULL)
//				{
//					TiXmlElement* startElement = startNode->FirstChildElement();
//					TiXmlAttribute *IDAttribute = startNode->FirstAttribute();
//					std::cout << IDAttribute->Value()<< std::endl;//cout the id.
//					while(startElement != NULL)
//					{					
//						std::cout << startElement->FirstChild()->Value() << std::endl;//cout the value.
//						startElement = startElement->NextSiblingElement();//point to next value.
//					}
//					startNode = startNode->NextSiblingElement();//point to next line in sql.
//				}
//			}
//
//
//		}
//
//		catch (std::string& e)
//		{
//			return false;
//		}
//
//		return true;*/
//
//		TiXmlElement* rootElement = _myXml->RootElement();
//		if(rootElement != NULL)//check the xml file isn't null.
//		{
//			std::cout << rootElement->Value() << std::endl;
//			TiXmlElement* startNode = rootElement->FirstChildElement();//startNode is the root of every Event data.	
//
//			if(startNode == NULL)
//				return false;
//
//			unsigned long temCounter = atol(startNode->FirstChild()->Value());
//			EventManager::setUidCounter(temCounter);
//
//			startNode = startNode ->NextSiblingElement();
//			while(startNode != NULL)
//			{
//				//initialize local variables for a certain Event.
//				std::string type, name, description;
//				unsigned int userID, eventID;
//				Time createTime, startTime, finishTime;
//				Reminding reminding;
//				TiXmlAttribute *IDAttribute = startNode->FirstAttribute();
//				eventID = std::atol(IDAttribute->Value());//assign eventID.
//				TiXmlElement* startElement = startNode->FirstChildElement();//startElement is the first element of the startNode.
//				if(startElement != startNode->LastChild() && startElement != NULL)//not null and it isn't reminding.
//				{	
//					type = startElement->FirstChild()->Value();//assign type.
//					startElement = startElement->NextSiblingElement();//point to next value.
//					userID = std::atol(startElement->FirstChild()->Value());//assign userID.
//					startElement = startElement->NextSiblingElement();//point to next value.
//					name = startElement->FirstChild()->Value();//assign name.
//					startElement = startElement->NextSiblingElement();//point to next value.
//					createTime = stringToTime(startElement->FirstChild()->Value());//assign createTime.
//					startElement = startElement->NextSiblingElement();//point to next value.
//					startTime = stringToTime(startElement->FirstChild()->Value());//assign startTime.
//					startElement = startElement->NextSiblingElement();//point to next value.
//					finishTime = stringToTime(startElement->FirstChild()->Value());//assign finishTime.
//					startElement = startElement->NextSiblingElement();//point to next value.
//					description = startElement->FirstChild()->Value();//assign description.
//					startElement = startElement->NextSiblingElement();//point to next value.
//				}
//				else if(startElement != NULL)
//				{
//					TiXmlElement* remindingElement = startElement->FirstChildElement();//remindingElement is is the first element of reminding.
//					reminding.setStartTime(stringToTime(remindingElement->FirstChild()->Value()));//assign startTime.
//					remindingElement = remindingElement->NextSiblingElement();//point to next value.
//					reminding.setRemindingInterval(atol(remindingElement->FirstChild()->Value()));//assign remindInterval.
//					remindingElement = remindingElement->NextSiblingElement();//point to next value.
//					reminding.setRemindTimes(atol(remindingElement->FirstChild()->Value()));//assign remindInterval.
//				}
//				startNode = startNode->NextSiblingElement();//point to next Event data in xml file.
//				EventManager::getSingleton().addEvent(type, name, startTime, finishTime, description, userID, eventID);
//			}	
//
//			return true;
//		}
//
//		return false;
//	}
//
//	//----------------------------------------------------------------------------//
//	bool XMLParserEvent::saveXmlFile()
//	{
//		try
//		{
//			if(_myXml->LastChild() != NULL)
//			{
//				_myXml->LastChild()->Clear();
//			}
//			insertUidCounterNode(EventManager::getUidCounter());
//
//			EventManager::EventIterator eventIt = EventManager::getSingleton().getIterator();
//			while(!eventIt.isAtEnd())
//			{
//				Event* temEvent = eventIt.getCurrentValue();
//				insertEventNode(eventIt.getCurrentValue(),eventIt.getCurrentValue()->getEventID());
//				++eventIt;
//			}
//			std::string seperator = "\\";
//			std::string fullPath = _appPath +seperator+ _xmlName;
//			_myXml->SaveFile(fullPath.c_str());
//		}
//
//		catch(std::string& e)
//		{
//			return false;
//		}
//		return true;
//	}
//	//----------------------------------------------------------------------------//
//	TiXmlElement* XMLParserEvent::insertNode(std::string ID)
//	{
//		TiXmlElement* classElement = new TiXmlElement(_className);
//		_myXml->RootElement()->LinkEndChild(classElement);
//		classElement->SetAttribute(_className + "ID", ID);
//		return classElement;
//	}
//
//	void XMLParserEvent::insertUidCounterNode(unsigned long counter)
//	{
//		TiXmlElement* counterElement = new TiXmlElement(_className + "UidCounter");
//		_myXml->RootElement()->LinkEndChild(counterElement);
//		char counterStr[100];
//		_ltoa_s(counter, counterStr, 10);
//		TiXmlText *aContent = new TiXmlText(counterStr);
//		counterElement->LinkEndChild(aContent);
//	}
//}		
